var roleVm = new Vue({
    el: "#roleTable",
    data: function () {
        return {
            page: {
                current: 1,
                size: 10,
                total: 0,
                records: [],
                condition: {
                    isEnable: "",
                    roleName: ""
                },
                other: {
                    forbidden: 0,
                    notForbidden: 0
                }
            },
            tableHeight: "",
            curFilterVal: "",
            listLoading: false,
            sels: []
        }
    },
    computed: {
        curFilter: function () {
            if (this.curFilterVal === "1") {
                return "启用(" + this.page.other.notForbidden + ")";
            } else if (this.curFilterVal === "0") {
                return "禁用(" + this.page.other.forbidden + ")";
            } else {
                return "全部(" + (this.page.other.forbidden+this.page.other.notForbidden) + ")";
            }
        },
        filtersStatusCount: function () {
            return [{
                text: "全部 (" + (this.page.other.forbidden+this.page.other.notForbidden) + ")",
                value: ""
            }, {
                text: "启用 (" + this.page.other.notForbidden + ")",
                value: "1"
            }, {
                text: "禁用 (" + this.page.other.forbidden + ")",
                value: "0"
            }]
        }
    },
    watch: {
        'page.condition.roleName': function (val) {
            this.filterTable(val, this.curFilterVal);
        },
    },
    methods: {
        selectable: function (row, index) {
            return !row.default;
        },
        getData: function () {
            VanyAjax("/sys/role/getRoleInfo", {
                current:this.page.current,
                size:this.page.size,
                condition:this.page.condition,
            }, "GET", ".el-table").done(function (data) {
                this.page.current = data.current;
                this.page.size = data.size;
                this.page.total = data.total;
                this.page.records = data.records;
                this.page.other = data.other;
                VanyAjax.prototype.hideLoading();
            }, false, this);
        },
        handleCurrentChange: function (val) {
            this.page.current = val;
            this.getData();
        },
        handleSizeChange: function (val) {
            this.page.size = val;
            this.getData();
        },
        filterTable: function (val) {
            this.getData();
        },
        filterStatus: function (command) {
            if (command == this.page.condition.isEnable) {
                return false;
            }
            this.page.condition.isEnable = command;
            this.curFilterVal = command;
            this.filterTable();
        },
        resetPaginationParams: function () {
            this.tableData = [];
            this.tableData_ = [];
            this.pageIndex = 1;
            this.nodataFlag = false;
            this._index = 0;
        },
        showAddRole: function () {
            addRole.isEdit = false;
            addRole.roleForm.id = "";
            addRole.show();
        },
        editRole: function (index, row) {
            addRole.roleForm.id = row.id;
            addRole.roleForm.roleName = row.roleName;
            addRole.roleForm.description = row.description;
            addRole.roleForm.isEnable = row.isEnable;
            addRole.isEdit = true;
            addRole.show();
        },
        deleteRole: function (row) {
            if(row.admin){
                this.$alert('无法删除系统管理员角色！请取消勾选。', '提示', {
                    type: 'warning',
                    customClass: "notLoadingThis"
                });
                return false;
            }
            if(row.default){
                this.$alert('无法删除默认角色！请取消勾选。', '提示', {
                    type: 'warning',
                    customClass: "notLoadingThis"
                });
                return false;
            }
            var self = this;
            this.$confirm('确认删除所选角色？', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(function () {
                VanyAjax("/sys/role/deleteRole", {
                    ids: row.id
                }).done(function (rspdata) {
                    self.getData();
                }, true, self);
            }).catch(function () {
            });
        },
        rolePerson: function (row) {
            rolePerson.params.roleId = row.id;
            rolePerson.show(row.roleName);
            rolePerson.getData();
        },
        // 分配功能权限
        showRoleFunctionTree: function (row) {
            roleFunctionTree.show();
            setTimeout(function () {
                roleFunctionTree.initTransfer({
                    roleId: row.id
                });
            }, 100);
        },
        // 批量删除
        remove: function () {
            if (this.sels.length === 0) {
                this.$alert('请选择要删除的角色。', '提示', {
                    type: 'warning',
                    customClass: "notLoadingThis"
                });
                return false;
            }
            var ids = this.sels.map(function (role) {
                return role.id;
            }).join(",");
            this.deleteRole(ids);
        },
        // 勾选角色的时候
        handleSelectionChange: function (selection) {
            this.sels = selection;
        }
    },
    mounted: function () {
        var self = this;
        this.$el.style.left = "210px";
        this.tableHeight = window.innerHeight - 320;
        this.getData();
    },
    updated: function () {
        Jurisdiction();
    }
});

/*添加角色*/
var addRole = new Vue({
    el: "#addRole",
    data: function () {
        return {
            treeAddVisible: false,
            labelPosition: 'right',
            isEdit: false,
            roleForm: {
                id: "",
                roleName: "",
                description: "",
                isEnable: "1"
            },
            rules: {
                roleName: [{
                    required: true,
                    message: '请输入角色名称',
                    trigger: 'blur'
                }]
            }
        };
    },
    computed: {
        isEditAdmin: function () {
            return this.roleForm.id !== "ADMINISTRATORS";
        }
    },
    methods: {
        submitForm: function (formName, e) {
            var self = this;
            this.$refs[formName].validate(function (valid) {
                if (valid) {
                    var url = self.isEdit ? "/sys/role/modifyRoleInfo" : "/sys/role/addRole";
                    VanyAjax(url, self.roleForm).done(function (data) {
                        this.hide();
                        this.resetForm("roleForm");
                        roleVm.getData();
                    }, true, self);
                } else {
                    VanyAjax.prototype.hideLoading();
                    e.stopPropagation();
                    return false;
                }
            });
        },
        show: function () {
            this.treeAddVisible = true;
        },
        hide: function () {
            this.treeAddVisible = false;
        },
        //重置
        resetForm: function (formName) {
            try {
                this.$refs[formName].resetFields();
            } catch (e) {

            }
            this.roleForm.id = "";
            this.roleForm.roleName = "";
            this.roleForm.description = "";
            this.roleForm.isEnable = "1";
        }
    },
    updated: function () {
        Jurisdiction();
    }
});

//功能权限树
var roleFunctionTree = new Vue({
    el: "#roleFunctionTree",
    data: function () {
        return {
            visible: false,
            roleId:"",
            treeData: [],
            filterText: "",
            defaultProps: {
                children: "children",
                label: "nameCh"
            }
        }
    },
    computed: {
        style: function () {
            if (window.innerHeight <= 768) {
                return {
                    height: "350px",
                    "overflow-y": "auto"
                }
            } else {
                return {
                    height: "500px",
                    "overflow-y": "auto"
                }
            }
        }
    },
    watch: {
        filterText: function (val) {
            this.$refs.tree.filter(val);
        }
    },
    methods: {
        show: function () {
            this.treeData = [];
            this.visible = true;
        },
        hide: function () {
            this.visible = false;
        },
        filterNode: function (value, data) {
            if (!value) {
                return true;
            } else {
                try {
                    return data[this.defaultProps.label].indexOf(value) !== -1;
                } catch (e) {
                    return true;
                }
            }
        },
        initTransfer: function (params) {
            this.roleId = params.roleId;
            VanyAjax("/sys/role/loadFunctionByRoleCode", params, "GET").done(function (data) {
                this.treeData = data.ownMenuList;
                var checkedKeys = data.allotIds;
                this.$refs["tree"].setCheckedKeys(checkedKeys);
            }, false, this);
        },
        save: function () {
            var roleId = this.roleId;
            var checkedKeys = this.$refs["tree"].getCheckedKeys().join(",");
            VanyAjax("/sys/role/modifyRoleFunctionAllotAndRemove", {
                roleId: roleId,
                menuIds: checkedKeys
            }).done(function (data) {
                this.hide();
            }, true, this);
        }
    },
    updated: function () {
        Jurisdiction();
    }
});
